<template>
    <div class="import-container">
        <el-form ref="form" label-width="130px">
            <div class="title">方案列表</div>
            <el-row>
                <el-checkbox-group v-model="checkedSolutions">
                    <el-col
                        v-for="item in solutionList"
                        :key="item.solutionId"
                        :xs="24"
                        :sm="12"
                        :md="8"
                        :lg="8"
                        :xl="6">
                        <el-checkbox v-model="item.solutionId" :label="item.solutionId">{{ item.solutionName }}</el-checkbox>
                    </el-col>
                </el-checkbox-group>
            </el-row>
            <div class="title">公式列表</div>
            <el-form-item>
                <el-button type="primary" @click="toExcRule">开始执行</el-button>
                <el-button @click="chooseAll">全选</el-button>
                <el-button @click="inverse">反选</el-button>
                <el-button>取消</el-button>
            </el-form-item>
            <el-row>
                <el-checkbox-group v-model="checkedRules">
                    <el-col
                        v-for="item in formField"
                        :key="item.ruleId"
                        :xs="24"
                        :sm="12"
                        :md="8"
                        :lg="8"
                        :xl="6">
                        <el-checkbox v-model="item.ruleId" :label="item.ruleId">{{ item.ruleName }}</el-checkbox>
                    </el-col>
                </el-checkbox-group>
            </el-row>
        </el-form>
    </div>
</template>

<script>
import { getExcSolutionByFormId, getSysRuleBySolutionId, checkAndExcSolutionParams, excRule } from '@/api/api'
import Cookies from 'js-cookie'
const session = JSON.parse(Cookies.get('sessionData'))
export default {
    name: 'ExcSolution',
    props: {
        excSolutionData: Object
    },
    data() {
        return {
            form: {
                name: ''
            },
            solutionList: [],
            formField: [],
            checkedRules: [],
            allRules: [],
            checkedSolutions: []
        }
    },
    created() {
        this.init()
    },
    methods: {
        init() {
            getExcSolutionByFormId(this.excSolutionData.formButtonId, this.excSolutionData.formId)
                .then(res => {
                    console.log(res)
                    this.checkedSolutions.push(res[0].solutionId)
                    this.solutionList = res
                    this._getSysRuleBySolutionId(res[0].solutionId)
                })
        },
        _getSysRuleBySolutionId(id) {
            getSysRuleBySolutionId(id)
                .then(res => {
                    res.forEach((item, index) => {
                        item.check = true
                    })
                    this.formField = res
                    this.formField.forEach(item => {
                        this.allRules.push(item.ruleId)
                    })
                })
        },
        chooseAll() {
            this.formField.forEach(item => {
                this.checkedRules.push(item.ruleId)
            })
        },
        getArrDifference(arr1, arr2) {
            return arr1.concat(arr2).filter(function(v, i, arr) {
                return arr.indexOf(v) === arr.lastIndexOf(v)
            })
        },
        inverse() {
            this.checkedRules = this.getArrDifference(this.checkedRules, this.allRules)
            // this.formField.forEach(item => {
            //     item.check = !item.check
            // })
        },
        checkParam() {
            let flag = false
            const ruleIds = this.checkedRules.join(',')
            checkAndExcSolutionParams(ruleIds, this.excSolutionData.formId, this.excSolutionData.formButtonId)
                .then(res => {
                    flag = res
                })
                .catch(err => {
                    console.log(err)
                })
            return flag
        },
        toExcRule() {
            if (this.checkedSolutions === null || this.checkedSolutions.length === 0) {
                alert('请至少选择一个方案')
                return
            }

            if (this.checkedRules === null || this.checkedRules.length === 0) {
                alert('请至少选择一个公式')
                return
            }

            if (this.checkParam()) {
                this.excRule()
            } else {
                this.excRule()
                // this.startExcRule()
            }
        },
        excRule() {
            const ruleIds = this.checkedRules.join(',')
            excRule(ruleIds, this.excSolutionData.formId, this.excSolutionData.formButtonId)
                .then(res => {
                    console.log(res)
                    alert(res)
                })
        },
        startExcRule() {
            console.log('11111')
        }
    }
}
</script>

